/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.dao.impl;

import static com.hh.bss.util.ObjectUtils.isNotEmpty;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.hh.bss.base.BaseHibernateDao;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.Page;
import com.hh.bss.project.dao.CustomerPartnerDao;
import com.hh.bss.project.entity.CustomerPartner;
import com.hh.bss.project.vo.query.CustomerPartnerQuery;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;

@Repository
public class CustomerPartnerDaoImpl extends BaseHibernateDao<CustomerPartner, Integer> implements CustomerPartnerDao<CustomerPartner, Integer> {

	public Class getEntityClass() {
		return CustomerPartner.class;
	}

	public Page findPage(CustomerPartnerQuery query) {

		Class[] os = { CustomerPartnerQuery.class };
		Class property = org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
		if (property != null) {
			if ("java.lang.String".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), query.getQuery());

			} else if ("java.lang.Integer".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));

			} else if ("java.lang.Double".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
			} else if ("java.util.Date".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
			}
		}
		List values = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ( ");
		sql.append("select a.id,a.ap_id apId,a.role,a.account,a.amount,a.percent,a.creator,a.create_dt createDt,a.modifier,a.modified_dt modifiedDt from customer_partner a ");
		sql.append(" ) t where 1=1 ");
		if (isNotEmpty(query.getId())) {
			sql.append(" and  t.id = ? ");
			values.add(query.getId());
		}
		if (isNotEmpty(query.getApId())) {
			sql.append(" and  t.apId = ? ");
			values.add(query.getApId());
		}

		return this.getJdbcDao().createPage(sql.toString(), values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}

	/**
	 * 检查是否有重复记录
	 */
	public boolean checkSameRoleAndAccount(CustomerPartner customerPartner) {
		StringBuffer sql = new StringBuffer(" select * from customer_partner a where  a.role = ? and a.account = ? and a.ap_id = ?  ");
		List values = new ArrayList();
		values.add(customerPartner.getRole());
		values.add(customerPartner.getAccount());
		values.add(customerPartner.getApId());
		List list = this.getJdbcDao().findByQuery(sql.toString(), values.toArray(), new MapObjectRowMapper());
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean validateOfferPartner(CustomerPartner customerPartner) {
		if (customerPartner != null) {
			if (StringUtils.isNotEmpty(customerPartner.getRole())) {
				// 如果是 职位负责人 和 合作顾问 验证业绩
				if ("MC".equals(customerPartner.getRole()) || "PC".equals(customerPartner.getRole())) {
					StringBuffer sql = new StringBuffer();
					List values = new ArrayList();
					//判断新增 和 修改 
					sql.append("select ifnull(sum(a.percent),0) percentSum from  customer_partner a where 1=1 and (a.role='MC' or a.role='PC') and a.ap_id = ?  ");
					
					values.add(customerPartner.getApId());
					if(customerPartner.getId() !=null){
						//修改
						sql.append(" and a.id <>  ?");
						values.add(customerPartner.getId());
					}
					try {
						Double percent = this.getJdbcDao().getJdbcTemplate().queryForObject(sql.toString(), values.toArray(), Double.class);
						logger.info("参数是:" + customerPartner.getPercent());
						logger.info("总数是:" + (percent + customerPartner.getPercent()));
						if ((customerPartner.getPercent() + percent) <= 100.00d) {
							return true;
						}
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}
		}
		return false;
	}

	@Override
	public boolean isExistMCRoleByJobId(Integer apId) {
		String hql = " from CustomerPartner a where a.role = 'MC' and a.apId = "+ apId;
		List list = this.getHibernateTemplate().find(hql);
		if(list != null && list.size() > 0 ){
			return true;
		}
		return false;
	}

}
